package com.webgis.utils;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Component;

@Component
public class WFSDescribeFeatureTypeGenerator {
    public String createWFSDescribeFeatureType() {
        Document document = DocumentHelper.createDocument();

        // 创建根元素并直接添加命名空间
        Element root = document.addElement("xsd:schema")
               .addNamespace("xsd", "http://www.w3.org/2001/XMLSchema")
               .addNamespace("gml", "http://www.opengis.net/gml")
               .addNamespace("test", "http://localhost:8888/test");

        root.addAttribute("elementFormDefault", "qualified");
        root.addAttribute("targetNamespace", "http://localhost:8888/test");
        root.addAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");

        Element importGml = root.addElement("xsd:import");
        importGml.addAttribute("namespace", "http://www.opengis.net/gml");
        importGml.addAttribute("schemaLocation", "http://schemas.opengis.net/gml/2.1.2/feature.xsd");

        Element szryType = root.addElement("xsd:complexType");
        szryType.addAttribute("name", "szryType");

        Element complexContent = szryType.addElement("xsd:complexContent");
        Element extension = complexContent.addElement("xsd:extension");
        extension.addAttribute("base", "gml:AbstractFeatureType");

        Element sequence = extension.addElement("xsd:sequence");

        // 添加所有属性字段
        addElementWithAttrs(sequence, "xsd:element",
                new String[]{"name", "type", "minOccurs", "nillable"},
                new String[]{"id", "xsd:double", "0", "true"});
        addElementWithAttrs(sequence, "xsd:element",
                new String[]{"name", "type", "minOccurs", "nillable"},
                new String[]{"objectid", "xsd:double", "0", "true"});
        // 添加其他属性字段...

        // 添加几何字段
        Element geomElement = sequence.addElement("xsd:element");
        geomElement.addAttribute("name", "geom");
        geomElement.addAttribute("type", "gml:PointPropertyType");
        geomElement.addAttribute("minOccurs", "0");
        geomElement.addAttribute("nillable", "true");

        Element annotation = geomElement.addElement("xsd:annotation");
        Element documentation = annotation.addElement("xsd:documentation");
        documentation.setText("EPSG:4326 (WGS84)");

        Element szryElement = root.addElement("xsd:element");
        szryElement.addAttribute("name", "szry");
        szryElement.addAttribute("substitutionGroup", "gml:_Feature");
        szryElement.addAttribute("type", "test:szryType");

        return document.asXML();
    }

    private Element addElementWithAttrs(Element parent, String name, String[] attrNames, String[] attrValues) {
        Element element = parent.addElement(name);
        for (int i = 0; i < attrNames.length; i++) {
            element.addAttribute(attrNames[i], attrValues[i]);
        }
        return element;
    }
}